package com.thrd.basicframework.utils;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatDialog;

import com.thrd.basicframework.R;


/**
 * @author: ${唐杰}
 * @date: 2021/3/9
 */
public class CommonDialog extends AppCompatDialog {



    private  CommonDialog mBaseDialog;
    private Builder mBuilder;

    private CommonDialog(Builder builder) {
        this(builder, R.style.MyDialogStyle);
    }

    private CommonDialog(Builder builder, int theme) {
        super(builder.mContext, theme);
        mBuilder = builder;
    }

    /**
     * 取消Dialog
     */
    public void dismissDialog() {
        mBaseDialog.dismiss();
        mBaseDialog = null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mBuilder.mView);
        setCanceledOnTouchOutside(mBuilder.cancelTouchout);
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            if (mBuilder.width > 0) {
                params.width = mBuilder.width;
            }
            if (mBuilder.height > 0) {
                params.height = mBuilder.height;
            }
            params.gravity = mBuilder.gravity;
            window.setAttributes(params);
        }
    }

    @Override
    public void show() {
        if(Looper.getMainLooper() != Looper.myLooper()) return;
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
//        if (!mBaseDialog.isShowing()) {
//            super.show();
//        }
        super.show();
        fullScreenImmersive(getWindow().getDecorView());
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

    }

    private void fullScreenImmersive(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(uiOptions);
        }
    }




    public static class Builder {
        private Context mContext;
        private boolean cancelTouchout = false;
        private int width, height;
        private int gravity;
        private View mView;
        private int resStyle = -1;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setView(int layoutId) {
            mView = LayoutInflater.from(mContext).inflate(layoutId, null);
            return this;
        }

        public Builder setView(View view) {
            mView = view;
            return this;
        }

        public Builder setGravity(int gravity) {
            this.gravity = gravity;
            return this;
        }

        public Builder setWidthdp(int val) {
            width = DensityUtil.dip2px(mContext,val);
            return this;
        }

        public Builder setHeightdp(int val) {
            height = DensityUtil.dip2px(mContext,val);
            return this;
        }



        public Builder setStyle(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder setCancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnClick(int viewRes, View.OnClickListener listener) {
            mView.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }

        public <T extends View> T findViewById(int id) {
            return mView.findViewById(id);
        }

        /**
         * 防止Dialog show两次
         */
        public CommonDialog build() {
//            if (mBaseDialog == null) {
                if (resStyle != -1) {
                    return new CommonDialog(this, resStyle);
                } else {
                    return new CommonDialog(this);
                }
//            }
//            return mBaseDialog;
        }
    }
}
